package com.toidep.fragment.mains;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.adapters.ArticleAdapter;
import com.toidep.api.ApiController;
import com.toidep.api.ParseJSON;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.entities.ArticleEntity;
import com.toidep.interfaces.ResponseListener;
import com.toidep.widgets.sectionlist.AmazingListView;

public class EventFragment extends BaseFragment {

	public boolean isRootFragment = true;
	private MainActivity mainActivity;

	private View view;
	private RelativeLayout layoutNoData;
	private LinearLayout layoutLoading;
	
	private TextView textview_event_name1, textview_event_name2, textview_event_name3, textview_event_description1;
	private ImageView imageview_event1, imageview_event2, imageview_event3;
	
	private AmazingListView amazingListView;
	private ArticleAdapter amazingAdapter;
	
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		mainActivity.currentCategory = Constants.CATEGORY_EVENT;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_event));
		mainActivity.mainFragment.showButtonSlide(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		mainActivity = (MainActivity) getActivity();
		view = inflater.inflate(R.layout.fragment_model_detail_tabs, container, false);

		initComponents();
		
		setBaseValues();
		
		requestData();
		
		return view;
	}

	private void initComponents() {
		
		((RelativeLayout) view.findViewById(R.id.layoutHeader)).setVisibility(View.GONE);
		
		layoutNoData = (RelativeLayout) view.findViewById(R.id.layoutNoData);
		layoutNoData.setVisibility(View.GONE);
		
		layoutLoading = (LinearLayout) view.findViewById(R.id.layoutLoading);
		layoutLoading.setVisibility(View.VISIBLE);
		
		// Khoi tao list model NEW
		amazingListView = (AmazingListView) view.findViewById(R.id.amazingListView);
		amazingListView.setAdapter(amazingAdapter = new ArticleAdapter(this, Constants.CATEGORY_EVENT, Constants.ARTICLE_MODE_EVENT));
		
		amazingAdapter.setAmazingListView(amazingListView, ConfigUtility.PAGE_SIZE);
		amazingListView.setLoadingView(getActivity().getLayoutInflater().inflate(R.layout.layout_loading_view, null));
		amazingListView.setDividerHeight(2);
		
		LinearLayout headerView =  (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.header_event, null);
		amazingListView.addHeaderView(headerView);
		
		textview_event_name1 = (TextView) headerView.findViewById(R.id.textview_event_name1);
		textview_event_name2 = (TextView) headerView.findViewById(R.id.textview_event_name2);
		textview_event_name3 = (TextView) headerView.findViewById(R.id.textview_event_name3);
		textview_event_description1 = (TextView) headerView.findViewById(R.id.textview_event_description1);
		
		imageview_event1 = (ImageView) headerView.findViewById(R.id.imageview_event1);
		imageview_event2 = (ImageView) headerView.findViewById(R.id.imageview_event2);
		imageview_event3 = (ImageView) headerView.findViewById(R.id.imageview_event3);
		
		// Chuyen toi detail cua video
		amazingListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (amazingAdapter != null && position >= 0) {
					amazingAdapter.onItemClick(position);
				}
			}
		});
	}
	
	/**
	 * Goi API get list events dau tien 
	 */
	private void requestData() {
		
		ApiController.getArticlesNormal(mainActivity, Constants.ARTICLE_MODE_EVENT,
				1, ConfigUtility.PAGE_SIZE, new ResponseListener() {
			
			@Override
			public void processResponse(int inintstall) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void processResponse(String response) {
				processData(response);
			}
		});
		
	}
	
	private void processData(String response) {
		AsyncTask<String, String, ArrayList<ArticleEntity>> asyn = new AsyncTask<String, String, ArrayList<ArticleEntity>>() {

			@Override
			protected ArrayList<ArticleEntity> doInBackground(String... params) {
				if (params[0] != null) {
					return ParseJSON.getAllArticlesObjectFromResponse(Constants.ARTICLE_MODE_EVENT, params[0]);
				}
				return null;
			}
			
			@Override
			protected void onPostExecute(ArrayList<ArticleEntity> lisNewsEntities) {
				setWidgetInfo(lisNewsEntities);
			}
			
		};
		
		asyn.execute(response);
	}
	
	protected void setWidgetInfo(ArrayList<ArticleEntity> lisNewsEntities) {
		layoutLoading.setVisibility(View.GONE);
		if (lisNewsEntities == null) {
			layoutNoData.setVisibility(View.VISIBLE);
			return;
		}
		
		int listSize = lisNewsEntities.size();
		if (lisNewsEntities == null || listSize == 0) {
			layoutNoData.setVisibility(View.VISIBLE);
			return;
		}
		
		ArticleEntity articleEntity1 = lisNewsEntities.get(0);
		int widthImage = ConfigUtility.size_full_width;
		int heightImage = (int) (widthImage*ConfigUtility.scale_size_horizontal_thumbs_item);

		int widthImageView = ConfigUtility.scrWidth;
		int heightImageView = (int) (widthImageView*ConfigUtility.scale_size_horizontal_thumbs_item);
		
		LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(widthImage, heightImage);
		
		mainActivity.loadImage(imageview_event1, articleEntity1.getImageUrl(), widthImage, heightImage, widthImageView, heightImageView);
		imageview_event1.setLayoutParams(param1);
		imageview_event1.setScaleType(ScaleType.FIT_CENTER);
		
		textview_event_name1.setText(articleEntity1.getTitle());
		textview_event_description1.setText(articleEntity1.getIntroText());

		int widthImage2 = ConfigUtility.size_half_width;
		int heightImage2 = (int) (widthImage2*ConfigUtility.scale_size_horizontal_thumbs_item);
		
		int widthImageView2 = ConfigUtility.scrWidth/2-10;
		int heightImageView2 = (int) (widthImageView2*ConfigUtility.scale_size_horizontal_thumbs_item);
		
		RelativeLayout.LayoutParams param2 = new RelativeLayout.LayoutParams(widthImageView2, heightImageView2);
		if (listSize >= 2) {
			ArticleEntity articleEntity2 = lisNewsEntities.get(1);
			
			mainActivity.loadImage(imageview_event2, articleEntity2.getImageUrl(), widthImage2, heightImage2, widthImageView2, heightImageView2);
			imageview_event2.setLayoutParams(param2);
			imageview_event2.setScaleType(ScaleType.FIT_CENTER);
			
			textview_event_name2.setText(articleEntity2.getTitle());
		} else {
			((LinearLayout) view.findViewById(R.id.layoutEvent2)).setVisibility(View.GONE);
			return;
		}
		
		if (listSize >= 3) {
			ArticleEntity articleEntity3 = lisNewsEntities.get(2);
			
			mainActivity.loadImage(imageview_event3, articleEntity3.getImageUrl(), widthImage2, heightImage2, widthImageView2, heightImageView2);
			imageview_event3.setLayoutParams(param2);
			imageview_event3.setScaleType(ScaleType.FIT_CENTER);
			
			textview_event_name3.setText(articleEntity3.getTitle());
		}  else {
			imageview_event3.setVisibility(View.GONE);
			textview_event_name3.setVisibility(View.GONE);
			return;
		}
		
		if (listSize >= 4) {
			amazingAdapter.notifyMayHaveMorePages();
			
			amazingAdapter.setAvatarImageSize();
			
			ArrayList<ArticleEntity> subList = new ArrayList<ArticleEntity>();
			subList.addAll(lisNewsEntities.subList(3, listSize));			
			amazingAdapter.resetList(subList);
		}
	}
	
	public static EventFragment newInstance() {
		EventFragment myFragment = new EventFragment();
		
		myFragment.setRootFragment(true);
		return myFragment;
	}

}
